package gdconf

import (
	"server_cluster/common/logger"
)

// 邮件配置表

const (
	MailSendTypeNone = 0 // 不发送
	MailSendTypeFree = 1 // 无条件发送
	MailSendTypeTime = 2 // 定时发送
)

type MailConfig struct {
	MailId         int32    `csv:"ID" gorm:"primary_key;autoIncrement:false"` // 邮件id
	Title          string   `csv:"Title,omitempty"`                           // 邮件标题
	Content        string   `csv:"Detail,omitempty"`                          // 邮件内容
	Sender         string   `csv:"Sender,omitempty"`                          // 邮件发送者
	ItemList       ItemList `csv:"MailReward,omitempty"`                      // 道具列表
	Importance     bool     `csv:"Important,omitempty"`                       // 是否重要邮件
	SendType       int32    `csv:"SendType,omitempty"`                        // 发送类型
	SendTime       *Time    `csv:"Timing"`                                    // 定时发送时间
	SendExpireHour int32    `csv:"DeleteTime,omitempty"`                      // 定时发送过期小时数
	ExpireHour     int32    `csv:"SaveTime,omitempty"`                        // 过期小时数
}

func (m *MailConfig) TableName() string {
	return "config_mail"
}

func (g *GameDataConfig) saveMailConfig() {
	saveTableToDb[MailConfig](g.Db, readTable[MailConfig](g.CsvPathPrefix+"Mail.csv"))
}

func (g *GameDataConfig) loadMailConfig() {
	g.GameDataMaps.MailConfigMap = make(map[int32]*MailConfig)
	for _, mailConfig := range loadTableFromDb[MailConfig](g.Db) {
		g.GameDataMaps.MailConfigMap[mailConfig.MailId] = mailConfig
	}
	logger.Info("MailConfig Count: %v", len(g.GameDataMaps.MailConfigMap))
}

func GetMailConfig(mailId int32) *MailConfig {
	value, exist := CONF.GameDataMaps.MailConfigMap[mailId]
	if !exist {
		logger.Error("[ConfigNotFound] MailConfig, mailId: %v", mailId)
		return nil
	}
	return value
}

func GetMailConfigMap() map[int32]*MailConfig {
	return CONF.GameDataMaps.MailConfigMap
}
